import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== declare
const { $Lazy } = window
// ===================================================================== Lazy
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component

class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '开始条码',		name: 'start_code', 	half:true,		onValue:({value})=>{this.start_code = value} },
			{ label: '结束条码',		name: 'end_code', 		half:true, 		onValue:({value})=>{this.end_code = value},	
				btn:{ label:'生成', 	 onClick:value=>{this.generateCode()}}
			},
			{ label: '条码号',		name: 'spec_codes',  	full:true,		required:true,		type:'textarea' },
		],
		api: '',
		title: ''
	}
	
	start_code = ''
	end_code = ''
	spec_code = []
	submit = {}
	
	open = ({ uuid, api, title }) => {
		this.submit.spec_codes = ''
		this.spec_code = []
		this.setState({api, title})
		this.modal.open({ formRef: this.formRef })
		this.submit.uuid = uuid
		this.submit.action = 'spec_codes'
	}
	
	// 生成条码
	generateCode = () => {
		const {start_code, end_code } = this
		// if(start_code.length === 12 || start_code.length === 14 ){
		if(start_code.length <= 14 ){
			if(start_code && end_code && +start_code > +end_code ){
				return message.then(f=>f.warning('开始条码必须小于结束条码'))
			}else{
				// if(end_code.length === 12 || end_code.length === 14){
				if(end_code.length <= 14){
					const arr =[]
					for(var i = parseInt(start_code); i <= parseInt(end_code); i++){
						arr.push(i)
					}
					this.spec_code = Array.from(new Set([...this.spec_code, ...arr]))
					this.formRef.setValue({spec_codes: this.spec_code.join('\r')})
				}
			}
		}else{
			return message.then(f=>f.warning('开始条码不正确'))
		}
	}
	
	// 解析条码
	getSpec = () => {
		let {spec_codes} = this.formRef.getValue(['spec_codes'])
		spec_codes = spec_codes.match(/\d{1,14}/g)
		this.submit.spec_codes = Array.from(new Set(spec_codes)).join(',')
	}
	
	render(){
		const { api, title } = this.state
		return FormModal.call(this,{ title, labelWidth:'s5', width:800, clear:true, api, enter:false, onOk:this.getSpec })
	}
}

export default Index